from django.core.management.base import BaseCommand
from news.crawlers.gxbh_crawler import GXBHCrawler


class Command(BaseCommand):
    help = "爬取广西北海新闻网的新闻"

    def add_arguments(self, parser):
        parser.add_argument("--page", type=int, help="要获取的页码", default=1)
        parser.add_argument("--category", type=str, help="新闻分类", default=None)
        parser.add_argument("--limit", type=int, help="爬取数量限制", default=10)

    def handle(self, *args, **options):
        page = options["page"]
        category = options["category"]
        limit = options["limit"]

        self.stdout.write(
            self.style.SUCCESS(
                f"开始爬取新闻，分类: {category or '全部'}，数量限制: {limit}"
            )
        )

        crawler = GXBHCrawler()
        count = crawler.crawl_and_save(page=page, category=category, limit=limit)

        self.stdout.write(self.style.SUCCESS(f"爬取完成，成功保存 {count} 条新闻"))
